<?php

namespace catchAdmin\system\controller;

use catchAdmin\system\request\AppSettingRequest;
use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\system\model\AppSetting as AppSettingModel;
use catcher\exceptions\CatchException;
use catcher\exceptions\FailedException;
use think\exception\ValidateException;
use think\Response;

class AppSetting extends CatchController
{

    protected $appSettingModel;

    /**
     *
     * @time 2023/03/10 11:11
     * @param AppSettingModel $appSettingModel
     * @return mixed
     */
    public function __construct(AppSettingModel $appSettingModel)
    {
        $this->appSettingModel = $appSettingModel;
    }


    /**
     *
     * @time 2023/03/10 11:11
     * @param Request $request
     * @return Response
     */
    public function save(Request $request): Response
    {
        try {
            $postData = $request->param();
            validate(AppSettingRequest::class)->scene("create")->check($postData);
            return CatchResponse::success($this->appSettingModel->saveDevice($postData));
        } catch (ValidateException $validateException) {
            throw new FailedException($validateException->getMessage());
        } catch (\Exception $exception) {
            throw new FailedException($exception->getMessage());
        }
    }

    /**
     *
     * @time 2023/03/10 11:11
     * @param $id
     * @return Response
     */
    public function read($id): Response
    {
        if (empty(trim($id)) || $id === "null") {
            throw new FailedException("未检测到设备码");
        }
        return CatchResponse::success($this->appSettingModel->getDeviceInfo($id));
    }


}